﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class administrator_nick_chat : System.Web.UI.Page
{
    dulieuDataContext db=new dulieuDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            LoadNickChat();
        }
    }
    protected void LoadNickChat()
    {
        rptmnu.DataSource = from o in db.NickChats select o;
        rptmnu.DataBind();
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        var nick=new NickChat(){NickName = txtnickname.Text,DisplayName = txtdisplayname.Text,Decene = 0};
        db.NickChats.InsertOnSubmit(nick);
        db.SubmitChanges();
        System.Threading.Thread.Sleep(800);
        lblthongbao.Text = "Thêm thành công";
        LoadNickChat();
    }
    protected void Xoa(object s,ImageClickEventArgs e)
    {
        foreach (RepeaterItem item in rptmnu.Items)
        {
            CheckBox chk = (CheckBox) item.FindControl("chk");
            if (chk.Checked)
            {
                Label lblid = (Label) item.FindControl("lblid");
                var nick=(from o in db.NickChats where o.id == Convert.ToInt32(lblid.Text) select o).FirstOrDefault();
                db.NickChats.DeleteOnSubmit(nick);
                db.SubmitChanges();
            }
        }
        System.Threading.Thread.Sleep(800);
        lblthongbao.Text = "Xóa thành công";
        LoadNickChat();
    }
    protected void Update(object s, ImageClickEventArgs e)
    {
        int id = Convert.ToInt32(txtid.Text);
        var nick = (from o in db.NickChats where o.id == Convert.ToInt32(id) select o).SingleOrDefault();
        if (nick != null)
        {
            nick.NickName = txtnickname.Text;
            nick.DisplayName = txtdisplayname.Text;
        }
        db.SubmitChanges();
        System.Threading.Thread.Sleep(800);
        lblthongbao.Text = "Cập nhật thành công";
        LoadNickChat();
    }
    protected void UpdateCheck(object s, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "edit")
        {
            var lblid = (Label)e.Item.FindControl("lblid");
            var nick = (from o in db.NickChats where o.id == Convert.ToInt32(lblid.Text) select o).SingleOrDefault();
            if (nick != null)
            {
                txtnickname.Text=nick.NickName;
                txtdisplayname.Text = nick.DisplayName;
                txtid.Text = lblid.Text;
            }
            System.Threading.Thread.Sleep(800);
           LoadNickChat();
        }
    }
}